<template>
    <div class="departmentMain" v-if='!loading'>
	    <zxz-uni-data-select v-model="deptId" :filterable='!isdisabled' :localdata="departArray" placeholder="请选择门诊"
	    	@change="confirm" :clear="(!isdisabled&&!isSelect)" :disabled='isdisabled'></zxz-uni-data-select>
      
    </div>
</template>
<script>
import {
		departmanetApi
	} from '@/api/department/department.js'


export default {
    name: 'department',
    props: ['isSelect'],
    data() {
        return {
			loading:true,
			deptId:'',
           allDeptArray:[],
		   departArray:[],//门诊集合
		   isdisabled:false,
        }
    },
    components: {  },
    computed: {
      
    },
    mounted() {
		this.loading=true
        this.getListAllDept()
    },
    watch: {
       
    },
    methods: {
		confirm(e){//确认执行筛选
			let data=e
			console.log(data)
			this.$emit('refreshData',data)
		},
      getListAllDept(){
		 departmanetApi.getlistAllDept().then(
		  	(response) => {
		  		if (response.code == 200) {
		  			let data=response.data
				    this.allDeptArray=data
					let deptId,type
					//判断 当前身份是医生 还是顾客 isPatient 0 医生  1顾客
					if(uni.getStorageSync('isPatient')==1){ // 顾客的时候取 patientHospital 中deptId 筛选出对应医院的门诊
						deptId=uni.getStorageSync('patientHospital').deptId  //查parentId
						type=1
					}else{// 医生身份
				
					let userObj=JSON.parse(uni.getStorageSync('user'))
					console.log(userObj)
					    //先判断医生是属于门诊医生还是部门医生  user  中的 dept   parentId 是否等于100  等100怎么为医院医生
						if(userObj.dept.parentId==100){ //查照这个   医院id=deptId 可以切换
							 deptId=userObj.dept.deptId  // 查parentId
							 type=1
						}else{ //门诊医生  则不能切换门诊  查找 deptId==deptId
							deptId=userObj.dept.deptId  //查deptId
							type=2
						}
					}
					this.getdepartArrayEvent(deptId,type)
		  		} else {
		  			this.$modal.showToast(response.msg)
		  		}
		  	}
		  )
	  },
    // 获取过滤之后的数组
	getdepartArrayEvent(deptId,type){//type==1  判断 deptId==parentId    2     判断 deptId==deptId
	   let data=this.allDeptArray
	   let dataArray=[]
		data.forEach(function(ele) {
			if(type==1){
				if(ele.parentId==deptId){
					let obj={
						text:ele.deptName,
						value:ele.deptId
					}
					dataArray.push(obj)
				}
			}else if(type==2){
				if(ele.deptId==deptId){
					let obj={
						text:ele.deptName,
						value:ele.deptId
					}
					dataArray.push(obj)
				}
			}
		})
		if(dataArray&&dataArray.length>0){
			if(dataArray.length==1){
				this.isdisabled=true
			}else{
				this.isdisabled=false
			}
		    this.departArray=dataArray
			this.deptId=dataArray[0].value
				console.log(this.deptId +'**********************//////////////////////////')
			this.$emit('refreshData',this.deptId)
		}
		
	
		console.log(this.departArray)
		this.loading=false
		
		
	}

    },
}
</script>

<style lang="scss" scoped>
.departmentMain{
	width:100%;
}
.departmentMain /deep/ .uni-select--disabled{
	background:transparent;
}
.departmentMain /deep/ .uni-select{
	font-size:24rpx;
	height:24px;
	border:none;
	min-height:24px;
	padding:0px;
}
.departmentMain	/deep/ .uni-select__input-box{
			height:24px;
		}
	
.departmentMain /deep/ .uni-select--disabled .uni-select__input-text{
	color:#C5C8CB!important;
}
</style>

